
package com.ruoyi.web.controller.bbsAdmin;

import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.bbsAdmin.domain.vo.commentVo;
import com.ruoyi.bbsAdmin.mapper.CommentMapper;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.bbsAdmin.domain.Comment;
import com.ruoyi.bbsAdmin.service.ICommentService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 评论Controller
 * 
 * @author ruoyi
 * @date 2022-04-01
 */
@RestController
@RequestMapping("/bbsAdmin/comment")
public class CommentController extends BaseController
{
    @Autowired
    private ICommentService commentService;

    @Resource
    CommentMapper commentMapper;

    /**
     * 查询评论列表
     */
    @PreAuthorize("@ss.hasAnyRoles('xygly,admin')")
    @GetMapping("/list")
    public TableDataInfo list(Comment comment)
    {
        startPage();
        List<Comment> list = commentService.selectCommentList(comment);
        return getDataTable(list);
    }

    /**
     * 导出评论列表
     */
    @PreAuthorize("@ss.hasAnyRoles('xygly,admin')")
    @Log(title = "评论", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Comment comment)
    {
        List<Comment> list = commentService.selectCommentList(comment);
        ExcelUtil<Comment> util = new ExcelUtil<Comment>(Comment.class);
        util.exportExcel(response, list, "评论数据");
    }

    /**
     * 获取评论详细信息
     */
    @PreAuthorize("@ss.hasAnyRoles('xygly,admin')")
    @GetMapping(value = "/{commentid}")
    public AjaxResult getInfo(@PathVariable("commentid") String commentid)
    {
        return AjaxResult.success(commentService.selectCommentByCommentid(commentid));
    }

    /**
     * 新增评论
     */
    @PreAuthorize("@ss.hasAnyRoles('xygly,admin')")
    @Log(title = "评论", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Comment comment)
    {
        return toAjax(commentService.insertComment(comment));
    }

    /**
     * 修改评论
     */
    @PreAuthorize("@ss.hasAnyRoles('xygly,admin')")
    @Log(title = "评论", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Comment comment)
    {
        return toAjax(commentService.updateComment(comment));
    }

    /**
     * 修改评论状态
     */
    @PreAuthorize("@ss.hasAnyRoles('xygly,admin')")
    @Log(title = "修改评论状态", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/commentState")
    public AjaxResult editCommentState(@RequestBody Comment comment)
    {
        return toAjax(commentService.updateCommentState(comment));
    }


    /**
     * 删除评论
     */
    @PreAuthorize("@ss.hasAnyRoles('xygly,admin')")
    @Log(title = "评论", businessType = BusinessType.DELETE)
	@DeleteMapping("/{commentids}")
    public AjaxResult remove(@PathVariable String[] commentids)
    {
        return toAjax(commentService.deleteCommentByCommentids(commentids));
    }

    /**
     * 根据帖子ID获取评论
     */
    @PreAuthorize("@ss.hasAnyRoles('xygly,admin')")
    @GetMapping(value = "/id/{contentid}")
    public AjaxResult getCommentById(@PathVariable("contentid") String contentid)
    {
        return AjaxResult.success(commentService.getCommentsByContentid(contentid));
    }

    @GetMapping(value = "getHotComment")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "contentId", value = "帖子ID", required = true),
    })
    public AjaxResult getHotComment(String contentId)
    {
        List<Comment> commentList =commentService.getCommentsByContentid(contentId);
        // 找到点赞数最高的评论
//        Optional<Comment> hotComment = commentList.stream()
//                .max(Comparator.comparingLong(Comment::getUpnum));
        Optional<Comment> hotComment = commentList.stream()
                .max(Comparator.comparingLong(comment -> comment.getUpnum() + comment.getChildrennum()));
        if (hotComment.isPresent()) {
            return AjaxResult.success(hotComment.get()); // 返回点赞+子评论总数最高的评论
        } else {
            return AjaxResult.success("没有找到评论");
        }
    }

    /**
     * 根据帖子ID获取评论
     */
    @PreAuthorize("@ss.hasAnyRoles('xygly,admin')")
    @GetMapping(value = "/commentVoById/{contentid}")
    public AjaxResult getCommentVoById(@PathVariable("contentid") String contentid)
    {
        return AjaxResult.success(commentService.getCommentVosByContentid(contentid));
    }

    @ApiOperation("删除redis中缓存的置顶帖子")
    @GetMapping("/deleteCommentListInRedis")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "schoolId", value = "学校ID", required = true),
    })
    public Boolean deleteCommentListInRedis(int schoolId) {
        try {
            Boolean result = commentService.deleteCommentListInRedis(schoolId);
            return result;
        } catch (Exception e) {
            return false;
        }

    }

    @GetMapping("/listBySchool")
    public TableDataInfo getlistBySchool(commentVo commentVo)
    {
        startPage();
        List<commentVo> list = commentMapper.selectCommentListBySchool(commentVo);
        return getDataTable(list);
    }

}
